package com.ejd.web.vo.manage.goods;

import java.io.Serializable;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

import net.sf.cglib.beans.BeanCopier;
import com.ejd.utils.MessageFactory;
import com.ejd.utils.SpringFacesUtil;
import com.ejd.utils.ValidatorUtil;
import com.ejd.web.bo.impl.Customer;
import com.ejd.web.constant.EjdConstants;
import com.ejd.web.constant.ManageBeanConstants;
import com.ejd.web.constant.SpringBeanConstants;
import com.ejd.web.vo.common.ConsigneeVo;
import com.ejd.web.vo.common.CustomerVo;
import com.ejd.web.vo.common.GoodsPropertiesVo;

@ManagedBean(name="updateGoodsPropertiesPage")
@SessionScoped
public class UpdateGoodsPropertiesPageBean extends GoodsPropertiesCrudBean implements Serializable {

	private static final long serialVersionUID = 5054087821033164848L;
	public UpdateGoodsPropertiesPageBean(){
	}

	public String saveGoodsPorperties() {
		if (null == this.getErrorMessages()) {
			this.setErrorMessages(new ArrayList<String>());
		}
		this.getErrorMessages().clear();
		// create new record
		UpdateGoodsPageBean updateGoodsPage = (UpdateGoodsPageBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.UPDATE_GOODS_PAGE_BEAN_NAME);
		if (EjdConstants.ACTION_NEW.equals(this.getActionOfProperties())) {
			validateInputDataNotEmpty(this.getGoodsPropertiesVo(),this.getErrorMessages());
			if (null != this.getErrorMessages() && this.getErrorMessages().size() > 0){
				return null;
			}
			List<GoodsPropertiesVo> tempGoodsProperties = updateGoodsPage.getGoods().getPropertiesv();
			if (null == tempGoodsProperties) {
				tempGoodsProperties = new ArrayList<GoodsPropertiesVo>();
			}
			
			BeanCopier copyHere = BeanCopier.create(GoodsPropertiesVo.class,GoodsPropertiesVo.class,false);
			GoodsPropertiesVo newGoodsPropertiesVo = new GoodsPropertiesVo();
			copyHere.copy(this.getGoodsPropertiesVo(), newGoodsPropertiesVo, null);
			Iterator<GoodsPropertiesVo> it = tempGoodsProperties.iterator();
			boolean exist = false;
			GoodsPropertiesVo  curProperties = null;
			while (it.hasNext()) {
				curProperties = it.next();
				if (curProperties.getColor().equals(newGoodsPropertiesVo.getColor()) && curProperties.getSize().equals(newGoodsPropertiesVo.getSize())) {
					exist = true;
					break;
				}
			}
			if (exist) {
				curProperties.setNumber(curProperties.getNumber()+ newGoodsPropertiesVo.getNumber());
			} else {
				tempGoodsProperties.add(newGoodsPropertiesVo);
			}
		}
		if (EjdConstants.ACTION_EDIT.equals(this.getActionOfProperties())) {
			validateInputDataNotEmpty(this.getGoodsPropertiesVo(),this.getErrorMessages());
			if (null != this.getErrorMessages() && this.getErrorMessages().size() > 0){
				return null;
			}
			List<GoodsPropertiesVo> tempGoodsProperties = updateGoodsPage.getGoods().getPropertiesv();
			if (null == tempGoodsProperties) {
				tempGoodsProperties = new ArrayList<GoodsPropertiesVo>();
			}
			
			BeanCopier copyHere = BeanCopier.create(GoodsPropertiesVo.class,GoodsPropertiesVo.class,false);
			GoodsPropertiesVo newGoodsPropertiesVo = new GoodsPropertiesVo();
			copyHere.copy(this.getGoodsPropertiesVo(), newGoodsPropertiesVo, null);
			Iterator<GoodsPropertiesVo> it = tempGoodsProperties.iterator();
			boolean exist = false;
			GoodsPropertiesVo  curProperties = null;
			int i = 0;
			while (it.hasNext()) {
				curProperties = it.next();
				if (i == this.getSelectedRow()){
					i = i + 1;
					continue;// the same one record data;
				}
				if (curProperties.getColor().equals(newGoodsPropertiesVo.getColor()) && curProperties.getSize().equals(newGoodsPropertiesVo.getSize())) {
					exist = true;
					break;
				}
				i = i+1;
			}
			if (exist) {
				FacesMessage message;
				message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME, "goods.colorsize.same.error.message", FacesMessage.SEVERITY_ERROR);
				this.getErrorMessages().add(message.getDetail());
			} else {
				updateGoodsPage.getPropertiesDataTable().setRowKey(this.getSelectedRow());
				GoodsPropertiesVo editData = (GoodsPropertiesVo)updateGoodsPage.getPropertiesDataTable().getRowData();
				editData.setColor(this.getGoodsPropertiesVo().getColor());
				editData.setSize(this.getGoodsPropertiesVo().getSize());
				editData.setNumber(this.getGoodsPropertiesVo().getNumber());
				editData.setId(this.getGoodsPropertiesVo().getId());
			}
			if (null != this.getErrorMessages() && this.getErrorMessages().size() > 0){
				return null;
			} 
		}
		return null;
	}
	
}
